<?php

/**
 * PUSH任务　控制器
 *
 * @author mengyu.wang
 */

require_once 'CommonController.php';

class Admin_PushTaskController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_PushTask();
        $this->_productModel = new Application_Model_PartnerProduct();
        $this->_psModel = new Application_Model_PushSettings();
        $this->view->title = "管理中心";
        $this->username = Application_Model_UserLogin::getInstance()->getLoginedUserName();
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
    }
    
    /**
     * 添加PUSH任务
     * 
     * @return void
     */
    public function insertAction()
    {
        //PUSH任务添加页面
        $productCode = $this->_productCode;
        //初始化个操作系统安装数量
        $this->view->androidInstallTotal = $this->_model->getInstallNum($productCode,'android');
        $this->view->iphoneInstallTotal = $this->_model->getInstallNum($productCode,'iphone');
        $this->view->ipadInstallTotal = $this->_model->getInstallNum($productCode,'ipad');
        $this->view->versions = $this->_model->getAllVersions($productCode);
        $this->view->product = $this->_productModel->fetchRow("code = '{$productCode}'");
        $vars = array();
        
         //提交表单的处理　
        //收集表单信息
        $filter = new Zend_Filter_StripTags();
        $osNameArr = array();
        if (isset($_REQUEST['osName'])) {
              foreach ($_REQUEST['osName'] as $osName) {
        		$osNameArr[] = $osName;
        	  }
        }
  
        $vars['startTime'] = $filter->filter(trim($this->_request->getPost('startTime')));
        $vars['endTime'] = $filter->filter(trim($this->_request->getPost('endTime')));
        $vars['version'] = $filter->filter(trim($this->_request->getPost('version')));
        $vars['redirPage'] = $filter->filter(trim($this->_request->getPost('redirPage')));
        $vars['targetNum'] = $filter->filter(trim($this->_request->getPost('targetNum')));
        $vars['msgInfo'] = $filter->filter(trim($this->_request->getPost('msgInfo')));
        $vars['status'] = intval($filter->filter(trim($this->_request->getPost('status'))));
        $vars['addUser'] = $this->username;
        $vars['addTime'] = date('Y-m-d H:i:s');
        $vars['productCode'] = $productCode;
        
        if ($this->_request->isPost()) {
	        //通用检测 
	        if (count($osNameArr) == 0) {
	           exit($this->view->message('必须选择操作系统！', $this->view->projectUrl($vars)));
	        }
	        //取得参数
	        if ($vars['startTime'] == '') {
	            exit($this->view->message('必须选择开始时间！', $this->view->projectUrl($vars)));
	        }
	        if ($vars['endTime'] == '') {
	            exit($this->view->message('必须选择结束时间！', $this->view->projectUrl($vars)));
	        }
	        if ($vars['startTime'] >= $vars['endTime'] ) {
	            exit($this->view->message('开始时间必须早于结束时间！', $this->view->projectUrl( $vars )));    
	        }
	        
	        if ($vars['version'] == '') {
	        	exit($this->view->message('必须选择软件版本！', $this->view->projectUrl( $vars )));  
	        }
	        if ($vars['version'] == '') {
	        	exit($this->view->message('必须选择软件版本！', $this->view->projectUrl( $vars )));  
	        }
	        if ($vars['redirPage'] == '') {
	        	exit($this->view->message('必须选择跳转页面！', $this->view->projectUrl( $vars )));  
	        }
	        if ($vars['status'] == 1) {
	        	if ($vars['msgInfo'] == '' or strlen($vars['msgInfo']) > 65) {
	        		exit($this->view->message('必须填写push信息, 而且字符长度不能超过65！', $this->view->projectUrl( $vars )));  
	        	}
	        }
	        $num = 0;
	        $osStr = '';
	        foreach ($osNameArr as $osName) {
	        	$vars['osName'] = $osName;
	        	$this->_model->insert($vars);
	        	$osStr .= $osName.', ';
	        	++$num;
	        }
	        if ($osStr != '') {
	        	$osStr = substr($osStr, 0, strlen($osStr) - 2);
	        }
	         if ($vars['status'] == 1) {
	         	//发送审核通知邮件
	         	$vars['osName'] = $osStr;
	         	$this->_model->sendMail($vars);
	         	exit($this->view->message('成功添加到审核箱中!', $this->view->projectUrl(array('action'=>'insert'))));	
	         } else {
	         	exit($this->view->message('成功添加到草稿箱中！', $this->view->projectUrl(array('action'=>'insert'))));
	         }
        }
    }
    /**
     * 添加推送信息时候，修改APP版本号刷新装机数
     */
    public function changeVersionAction()
    {
    	$productCode = trim($_REQUEST['productCode']);
    	$version = trim($_REQUEST['version']);
    	$rows = $this->_model->getInstallNumByVersion($productCode, $version);
    	$json_string = json_encode($rows);
    	echo $json_string;
    	exit();
    }
    
    /**
     * 邮箱配置
     */
    public function mailSettingAction()
    {
    	$productCode = $this->_productCode;
    	$this->view->row = $this->_psModel->fetchRow("productCode = '{$productCode}'");
    	$vars = array();
        
        //收集表单信息
        $filter = new Zend_Filter_StripTags();
        $vars['projectGroup'] = $filter->filter(trim($this->_request->getPost('projectGroup')));
        $vars['yekExamineGroup'] = $filter->filter(trim($this->_request->getPost('yekExamineGroup')));
        $vars['partnerExamineGroup'] = $filter->filter(trim($this->_request->getPost('partnerExamineGroup')));
        $vars['yekIsExamine'] = intval($filter->filter(trim($this->_request->getPost('yekIsExamine'))));
        $vars['partnerIsExamine'] = intval($filter->filter(trim($this->_request->getPost('partnerIsExamine'))));
        $vars['productCode'] = $productCode;
    	if ($this->_request->isPost()) {
    	    if (!$vars['projectGroup'] or !$vars['yekExamineGroup']) {
        	 exit($this->view->message('请选择YEK和客户验证是否需要！', $this->view->projectUrl($vars)));
        	}
	    	if ($this->_psModel->existsSetting($productCode)) {
	    		$this->_psModel->update($vars, "productCode = '{$productCode}'");
	    	} else {
	    		$this->_psModel->insert($vars);
	    	}
	    	exit($this->view->message('修改成功！', $this->view->projectUrl(array('action'=>'mail-setting'))));
    	}
    }
}